package com.itany.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.itany.book.service.IBookService;

import com.itany.constant.ResponseConstant;
import com.itany.exception.RequestParamsException;
import com.itany.pojo.User;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;

/**
 * Created by tyh on 2018/9/23.
 */
@RestController
@RequestMapping("/book")
public class TestBookController {

    @Reference
    private IBookService bookService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping("/addDoc")
    public ActionResult addDoc() {
        ActionResult ac = new ActionResult();
        bookService.addDoc();
        ac.setStatus(0);
        return ac;
    }


    @RequestMapping("/findDoc")
    public List<Map<String, String>> findDoc(String keyWord) {
        return bookService.findDoc(keyWord);
    }


    /**
     * 查找所有的图书类别用于侧边栏的显示
     *
     * @return
     */
    @RequestMapping("/findAllTypes")
    @ResponseBody
    public ActionResult findAllTypes() {
        ActionResult ac = new ActionResult();
        try {
            Map<String, Object> allTypes = bookService.findAllTypes();
            ac.setData(allTypes);
            ac.setStatus(ResponseConstant.RESPONSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            ac.setStatus(ResponseConstant.RESPONSE_FAIL);
            ac.setMsg("服务器内部异常");
        }
        return ac;
    }

    ;


    @RequestMapping("/bookdetails/{id}")
    public ModelAndView findById(@PathVariable("id") String id) {
        ModelAndView mav = new ModelAndView();
        try {
            Map bookmap = bookService.findById(id);
            mav.addObject("book", bookmap);
            mav.setViewName("bookDetails");
            mav.setStatus(HttpStatus.OK);
        } catch (RequestParamsException e) {
            mav.setStatus(HttpStatus.BAD_REQUEST);
            mav.setViewName("bookList");
        }
        return mav;

    }

    @RequestMapping("/shoppingCar/{id}")
    @ResponseBody
    public ActionResult addShoppingCar(@PathVariable("id") String id, HttpServletRequest request) {
        ActionResult ac = new ActionResult();
        Cookie[] cks = request.getCookies();
        User user = null;
        if (null != cks) {
            for (Cookie ck : cks) {
                if ("TT_TOKEN".equals(ck.getName())) {
                    String token = ck.getValue();
                    String str = redisTemplate.opsForValue().get("USER_TOKEN::" + token);
                    user = JSONObject.parseObject(str, User.class);
                }
            }
            try {
                bookService.addShoppingCar(id, user);
                ac.setMsg("添加成功，可以继续购物了");
                ac.setStatus(ResponseConstant.RESPONSE_SUCCESS);
            } catch (RequestParamsException e) {
                ac.setMsg(e.getMessage());
                ac.setStatus(ResponseConstant.REQUEST_PARAMETER_ERROR);
            }catch (Exception e) {
                ac.setMsg("服务器内部异常");
                ac.setStatus(ResponseConstant.SERVER_ERROR);
            }
        } else {
            ac.setMsg("cookie已被禁用，请启用");
            ac.setStatus(ResponseConstant.RESPONSE_FAIL);
        }
        return ac;
    }

}
